//
//  XFDLogInInputView.m
//  XFDLogin_Example
//
//  Created by 韩冰 on 2017/11/3.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDLogInInputView.h"
#import <XFDCommon/HBCommon.h>
#import <Masonry/Masonry.h>
#import <XFDCommon/OYCountDownManager.h>
#import <BlocksKit/UIControl+BlocksKit.h>
#import <BlocksKit/UITextField+BlocksKit.h>
@interface XFDLogInInputView()
@property (nonatomic, strong)UITextField *textField;
@property (nonatomic, strong)UIView *lineView;
@property (nonatomic, strong)UIButton *codeButton;
@property (nonatomic, assign)BOOL isHasCodeBtn;
@end

@implementation XFDLogInInputView

- (instancetype)initWithFrame:(CGRect)frame withIsCodeButton:(BOOL)isHas{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.isHasCodeBtn = isHas;
        self.timeCount = 60;
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews{
    [self addSubview:self.textField];
    [self addSubview:self.lineView];
    [self addSubview:self.codeButton];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).priorityHigh();
        make.right.equalTo(self.mas_right).with.offset(-40).priorityHigh();
        make.left.equalTo(self.mas_left).with.offset(40).priorityHigh();
        make.height.mas_offset(1).priorityHigh();
    }];
    if (self.isHasCodeBtn) {
        [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.lineView.mas_top).priorityHigh();
            make.right.equalTo(self.mas_right).with.offset(-40).priorityHigh();
            make.width.mas_offset(90).priorityHigh();
            make.height.mas_offset(40).priorityHigh();
        }];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.lineView.mas_top).priorityHigh();
            make.right.equalTo(self.codeButton.mas_left).with.offset(-40).priorityHigh();
            make.left.equalTo(self.mas_left).with.offset(40).priorityHigh();
            make.height.mas_offset(40).priorityHigh();
        }];
    }else{
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.lineView.mas_top).priorityHigh();
            make.right.equalTo(self.mas_right).with.offset(-40).priorityHigh();
            make.left.equalTo(self.mas_left).with.offset(40).priorityHigh();
            make.height.mas_offset(40).priorityHigh();
        }];
    }
      [self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)setIsClear:(BOOL)isClear{
    _isClear = isClear;
    if (_isClear) {
         self.textField.clearButtonMode =UITextFieldViewModeWhileEditing;
    }else{
        self.textField.clearButtonMode =UITextFieldViewModeNever;
    }
   
}
- (void)setIsCiphertButton:(BOOL)isCiphertButton{
    _isCiphertButton = isCiphertButton;
    if (_isCiphertButton) {
        UIButton *ciphertButton = [UIButton buttonWithType:UIButtonTypeCustom];
        WEAKSELF
        [ciphertButton setImage:[UIImage hb_imagePathWithName:@"visible" bundle:@"XFDLogin" targetClass:[self class]] forState:UIControlStateNormal];
        [ciphertButton bk_addEventHandler:^(id sender) {
            STRONGSELF
            strongSelf.isCiphertext =!strongSelf.isCiphertext;
            if (strongSelf.isCiphertext) {
                [ciphertButton setImage:[UIImage hb_imagePathWithName:@"visible" bundle:@"XFDLogin" targetClass:[self class]]forState:UIControlStateNormal];;
                  }else{
                [ciphertButton setImage:[UIImage hb_imagePathWithName:@"invisible" bundle:@"XFDLogin" targetClass:[self class]]forState:UIControlStateNormal];;
                  }
        } forControlEvents:UIControlEventTouchUpInside];
        ciphertButton.frame = CGRectMake(0, 0, 40, 40);
        self.textField.rightView = ciphertButton;
         self.textField.rightViewMode =UITextFieldViewModeAlways;
        
    }else{
        
        self.textField.rightViewMode = UITextFieldViewModeNever;
    }

}

- (void)inputViewResignFirstResponder{
    [self.textField resignFirstResponder];
  
}
- (void)setIsCiphertext:(BOOL)isCiphertext{
    _isCiphertext = isCiphertext;
    self.textField.secureTextEntry = _isCiphertext;
}

- (void)inputViewBecomeFirstResponder{
      [self.textField becomeFirstResponder];
}


- (void)textFieldDidChange:(UITextField *)inputView{
    NSLog(@"%@",inputView.text );
    if (self.getTextFromeInputView)
    {
        self.getTextFromeInputView(inputView.text);
    }
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.textField.placeholder = self.placeholder;
}
- (void)setCodeBtnTitle:(NSString *)codeBtnTitle{
    _codeBtnTitle = codeBtnTitle;
    [self.codeButton setTitle:self.codeBtnTitle forState:UIControlStateNormal];
}
- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.font = [UIFont systemFontOfSize:14];
    }
    return _textField;
}
- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = GlobalLineColor;
    }
    return _lineView;
}
- (UIButton *)codeButton{
    if (!_codeButton) {
        _codeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _codeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        _codeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_codeButton setTitleColor:[UIColor HB_ColorWithHexString:@"#333333"] forState:UIControlStateNormal];
        WEAKSELF
        [_codeButton bk_addEventHandler:^(id sender) {
            STRONGSELF
            if (strongSelf.getCodeBlock) {
                strongSelf.getCodeBlock();
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeButton;
}

- (void)setDefultText:(NSString *)defultText{
    _defultText = defultText;
    self.textField.text =_defultText;
}
// 开启倒计时效果
-(void)openCountdown{
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [self.codeButton setTitle:@"重新发送" forState:UIControlStateNormal];
                self.codeButton.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [self.codeButton setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                self.codeButton.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

- (NSString *)inputText{
    return self.textField.text;
}


@end
